import axios from 'axios'
import memory from '../utils/memory'
import storage from '../utils/storage'

const request = axios.create({
    // baseURL: 'http://localhost:11120',
    // baseURL: 'http://47.107.130.239:11120',
    baseURL: 'http://uchk.808b.com:11100',
})



const unTokenUrl = ['/user/login', '/user/register', '/user/forget', '/resume/listone']

// 请求拦截器
request.interceptors.request.use(
    config => {
        if(unTokenUrl.includes(config.url)){
            return config
        }
        config.data.token = memory.user.token
        return config
    },
    error => {
        return Promise.reject(error)
    }
)


// 响应拦截器
request.interceptors.response.use(
    response => {
        if(response.status !== 200){
            return Promise.reject(response)
        }
        if(response.data.code === 314){
            alert('登录超时,请重新登录!')
            memory.user = {}
            storage.user.remove()
            window.location.reload()
        }
        return Promise.resolve(response)
    }, error => {
        return Promise.reject(error)
    }
)

export default request